<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Auth\AuthenticationException;

class AuthenticateWithGuard extends Authenticate
{

    public function handle($request, Closure $next, ...$guards)
    {
        $this->authenticate($request, $guards);

        return $next($request);
    }

    protected function authenticate($request, array $guards)
    {

        if (empty($guards)) {
            return $this->auth->authenticate($request);
        }

        foreach ($guards as $guard) {

            if ($this->auth->guard($guard)->check()) {
                $user = $this->auth->guard($guard)->user();

                if ($guard == 'api' && $user->token()->name == 'ExhibitorUser') {
                    return $this->auth->shouldUse($guard);
                }
            }else{
                dd(112);
            }
        }

        throw new AuthenticationException('Unauthenticated.', $guards);
    }

}
